#! /bin/bash

: ${PROG:=$(basename ${BASH_SOURCE})}

_complete_() {
    local options_with_args=$1
    local boolean_options="$2 -h --help"
    local transports=$3

    local option_with_args
    for option_with_args in $options_with_args $transports
    do
        if [ "$option_with_args" == "$prev" -o "$option_with_args" == "$cur" ]
        then
            return
        fi
    done

    case "$cur" in
        -*)
            COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) )
            ;;
        *)
            if [ -n "$transports" ]
            then
                compopt -o nospace
                COMPREPLY=( $( compgen -W "$transports" -- "$cur" ) )
            fi
            ;;
    esac
}

_skopeo_supported_transports() {
    local subcommand=$1

    ${PROG} $subcommand --help | grep "Supported transports" -A 1 | tail -n 1 | sed -e 's/,/:/g' -e 's/$/:/'
}

_skopeo_copy() {
    local options_with_args="
    --authfile
    --format -f
    --sign-by
    --src-creds --screds
    --src-cert-dir
    --src-tls-verify
    --dest-creds --dcreds
    --dest-cert-dir
    --dest-ostree-tmp-dir
    --dest-tls-verify
    --src-daemon-host
    --dest-daemon-host
    "

    local boolean_options="
    --dest-compress
    --remove-signatures
    "

    local transports="
    $(_skopeo_supported_transports $(echo $FUNCNAME | sed 's/_skopeo_//'))
    "

    _complete_ "$options_with_args" "$boolean_options" "$transports"
}

_skopeo_inspect() {
     local options_with_args="
     --authfile
     --creds
     --cert-dir
     "
     local boolean_options="
     --raw
     --tls-verify
    "

    local transports="
    $(_skopeo_supported_transports $(echo $FUNCNAME | sed 's/_skopeo_//'))
    "

    _complete_ "$options_with_args" "$boolean_options" "$transports"
}

_skopeo_standalone_sign() {
     local options_with_args="
       -o --output
     "
     local boolean_options="
     "
    _complete_ "$options_with_args" "$boolean_options"
}

_skopeo_standalone_verify() {
     local options_with_args="
     "
     local boolean_options="
     "
    _complete_ "$options_with_args" "$boolean_options"
}

_skopeo_manifest_digest() {
     local options_with_args="
     "
     local boolean_options="
     "
    _complete_ "$options_with_args" "$boolean_options"
}

_skopeo_delete() {
     local options_with_args="
     --authfile
     --creds
     --cert-dir
     "
     local boolean_options="
     --tls-verify
     "

    local transports="
    $(_skopeo_supported_transports $(echo $FUNCNAME | sed 's/_skopeo_//'))
    "

    _complete_ "$options_with_args" "$boolean_options" "$transports"
}

_skopeo_layers() {
     local options_with_args="
       --creds
       --cert-dir
     "
     local boolean_options="
       --tls-verify
     "
    _complete_ "$options_with_args" "$boolean_options"
}

_skopeo_skopeo() {
     local options_with_args="
       --policy
       --registries.d
       --override-arch
       --override-os
       --command-timeout
     "
     local boolean_options="
       --insecure-policy
       --debug
       --version -v
       --help -h
     "
     commands=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )

     case "$prev" in
         $main_options_with_args_glob )
             return
             ;;
     esac

     case "$cur" in
         -*)
             COMPREPLY=( $( compgen -W "$boolean_options $options_with_args" -- "$cur" ) )
             ;;
         *)
             COMPREPLY=( $( compgen -W "${commands[*]} help" -- "$cur" ) )
             ;;
     esac
}

_cli_bash_autocomplete() {
     local cur opts base

     COMPREPLY=()
     cur="${COMP_WORDS[COMP_CWORD]}"
     COMPREPLY=()
     local cur prev words cword

     _get_comp_words_by_ref -n : cur prev words cword

     local command=${PROG} cpos=0
     local counter=1
     counter=1
     while [ $counter -lt $cword ]; do
         case "${words[$counter]}" in
             -*)
                ;;
             *)
                 command=$(echo "${words[$counter]}" | sed 's/-/_/g')
                 cpos=$counter
                 (( cpos++ ))
                 break
                 ;;
         esac
         (( counter++ ))
     done

     local completions_func=_skopeo_${command}
     declare -F $completions_func >/dev/null && $completions_func

     eval "$previous_extglob_setting"
     return 0
}

 complete -F _cli_bash_autocomplete $PROG
